define(function(require, exports){
	var $ = require("jquery");
	var util = require('util');
	var pubjs = require('pubjs');
	var view  = require('@base/view');

	// 首选订单
	var Prev = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'Type': 1,
				'addText': LANG('添加首选交易订单'),
				'url': 'deal/list',
				'syncUrl': 'deal/sync',
				'view_model': {
					'showAdd': false
				}
			});

			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			self.append($([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('订单管理')+'</span>',
					'<span ms-visible="showAdd" class="ml10" pub-name="add" pub-mod="@base/common/drop.add"></span>',
				'</div>',
				'<div class="cl" pub-name="grid" pub-mod="deal/grid/list.base"/>'
			].join('')));


			var config = {
				'grid': {
					'param': {
						'Type': c.Type,
						'IsDeleted': 0
					},
					'eventDataLoad': true,
					'hasSearch': true
				},
				'add':{
					'def': c.addText,
					'href': '#deal/edit?Type=' + c.Type
				}
			};

			if(pubjs.checkRight('deal.add')) {
				self.vm.setItem('showAdd', true);
			}
			self.vm.setItem('showAdd', true);
			self.buildFromTemplate(config);

			self.createAsync('sidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'items': [
					{'name': 'status', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('状态：'),
						'data': [
							{id: 1, name: LANG('开启')}
							,{id: 2, name: LANG('关闭')}
						],
						'value': 1
					}},
					{'name': 'channel', 'uri':'modules/sideBarBase.channel','config':{
						'filterAccess': true
					}}
				]
			});

		},
		onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;
				switch (type) {
					case 'status':
						param.Status = +id;
						break;
					case 'channel':
						param.ChannelId = +id;
						break;
				}
			});

			self.reload(param);
		},
		onInputClick: function(ev){
			return false;
		},
		reload: function( param ) {
			this.$.grid.reload(param);
		},
		onOperateMenuShow: function(ev){
			var row = ev.param;
			var op = [];

			var user = pubjs.getUser();
			op = [];
			if(pubjs.checkRight('master.sunfei'))
			{
				op.push({name: LANG("同步"), id: 'sync'});
			}
			if(pubjs.checkRight('master.sunfei'))
			{
				op.push({name: LANG("编辑"), id: 'edit'});
			}

			if(!op.length)
			{
				op.push({name: LANG('没有操作'), id: ''});
			}

			ev.returnValue = op;
			return false;
		},
		// 操作菜单-响应菜单操作
		onOperateMenuSelect: function(ev){
			var self = this;
			var op = ev.param[0]; // 按钮信息：{key: 1, name: '开始'}
			var data = ev.param[1];

			switch(op.key){
				case 'sync':
					this.showLoading();
					pubjs.mc.send(self.getConfig('syncUrl'), {_id: data._id, AdxId: data.AdxId}, self.afterSync.bind(self));
					break;
				case 'edit':
					window.open('#deal/edit/'+ data._id + '?Type=' + self.getConfig('Type'));
					break;
				case 'view':
					break;
			}
			return false;
		},
		afterSync: function(err, data) {
			this.hideLoading();
			if(err) {
				pubjs.alert(err.message);
				return false;
			}

			pubjs.alert(data && data.message || LANG('同步成功'));
		}
	});
	exports.prev = Prev;

	// 私有订单
	var Priv = Prev.extend({
		init:function(config){
			config = pubjs.conf(config, {
				Type: 2
				,addText: LANG('添加私下竞价订单')
			});

			this.Super('init', arguments);
		}
	});
	exports.priv = Priv;

	// 私有订单
	var PDB = Prev.extend({
		init:function(config){
			config = pubjs.conf(config, {
				Type: 3
				,addText: LANG('添加PDB订单')
			});

			this.Super('init', arguments);
		}
	});

	exports.pdb = PDB;
});